// pages/grid4_chat/scroll-chat-area/index.js
const bottomViewId = 'scroll-area-bottom-view'
const formatTime = require('../../../utils/util.js').formatTime2
const webim = require('../../../utils/webim_wx.js')

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        messageList: Array
    },

    /**
     * 组件的初始数据
     */
    data: {
        // 用来设置滑动到最底部
        toView: null,

        // 顶部加载
        upperLoading: false,
    },

    ready() {},

    /**
     * 组件的方法列表
     */
    methods: {
        bindBackButtonTap() {
            this.scrollToBottom()
            this.setData({
                isScanningHistory: false
            })
        },
        scrollToBottom() {
            this.setData({
                toView: bottomViewId
            })
        },

        bindScrollToUpper() {
            this.setData({
                upperLoading: true
            })
            this.triggerEvent('forwardrefresh', {
                success: () => {
                    setTimeout(() => {
                        this.setData({
                            upperLoading: false
                        })
                    }, 1000)
                },
                failed: e => {
                    setTimeout(() => {
                        this.setData({
                            upperLoading: false
                        })
                        wx.showToast({
                            title: '拉取消息记录失败',
                            icon: 'none'
                        })
                    }, 1000)
                }
            })
        },
        // 点击语音消息 播放
        bindVoiceTap(e) {
            let dataset = e.target.dataset
            let url = dataset.voiceUrl,
                hasCached = Boolean(dataset.hasCached)
            wx.downloadFile({
                url,
                success: function(res) {
                    const filePath = res.tempFilePath

                    const innerAudioContext = wx.createInnerAudioContext()
                    innerAudioContext.autoplay = true
                    innerAudioContext.src = filePath
                    innerAudioContext.onPlay(() => {
                        console.log('开始播放')
                    })
                    innerAudioContext.onError((res) => {
                        console.log(res.errMsg)
                        console.log(res.errCode)
                    })
                },
                fail() {
                    wx.showToast({
                        title: '打开文件失败',
                        icon: 'none'
                    })
                }
            })


        },
        test(e) {}
    },

})